/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
import ordenacao.HeapSort;
import ordenacao.MergeSort;
import ordenacao.QuickSort;
import ordenacao.SimpleSort;

/**
 *
 * @author juks
 */
public class SortEngine implements Runnable {

    int ParametroN;
    int ParametroT;
    Interface view;

    public SortEngine(int n, int t, Interface v) {
        this.ParametroN = n;
        this.ParametroT = t;
        this.view = v;

        inicializaBarraProgresso();

    }

    private void inicializaBarraProgresso() {
        int AlgoritmosSelecionados = 0;


        if (this.view.getChkHeapSort().isSelected()) {
            AlgoritmosSelecionados++;
        }
        if (this.view.getChkMergesort().isSelected()) {
            AlgoritmosSelecionados++;
        }
        if (this.view.getChkQuisckSort().isSelected()) {
            AlgoritmosSelecionados++;
        }
        if (this.view.getChkbubbleSort().isSelected()) {
            AlgoritmosSelecionados++;
        }
        if (this.view.getChkinsertionSort().isSelected()) {
            AlgoritmosSelecionados++;
        }
        if (this.view.getChkinsertionSort().isSelected()) {
            AlgoritmosSelecionados++;
        }

        int totalBarra = AlgoritmosSelecionados * ParametroT;
        this.view.getPgbProgresso().setMaximum(totalBarra);
        this.view.getPgbProgresso().setValue(0);
        this.view.getPgbProgresso().setForeground(Color.orange);
        this.view.getPgbProgresso().repaint();
    }

    @Override
    public void run() {


        for (int i = 0; i < this.ParametroT; i++) {

            int[] vetor = geraVetor(this.ParametroN);

            adcionatexto("\n============================== Iniciando algoritmos ==============================");
            adcionatexto("\n -- iniciando teste:" + (i + 1) + "\n");
            if (this.view.getChkHeapSort().isSelected()) {
                System.out.println("HeapSort");
                this.HeapSort(copiaVetor(vetor));
            }
            if (this.view.getChkMergesort().isSelected()) {
                System.out.println("MergeSort");
                this.MergeSort(copiaVetor(vetor));
            }
            if (this.view.getChkQuisckSort().isSelected()) {
                System.out.println("QuickSort");
                this.QuickSort(copiaVetor(vetor));
            }
            if (this.view.getChkbubbleSort().isSelected()) {
                System.out.println("bubble");
                this.BubbleSort(copiaVetor(vetor));
            }
            if (this.view.getChkinsertionSort().isSelected()) {
                System.out.println("insert");
                this.InsertionSort(copiaVetor(vetor));
            }
            if (this.view.getChkinsertionSort().isSelected()) {
                System.out.println("Selection");
                this.SelectionSort(copiaVetor(vetor));
            }

        }
        this.view.getBtnIniciarTestes().setEnabled(true);
        adcionatexto("\n============================== Fim ==============================");

        try {
            this.finalize();
        } catch (Throwable ex) {
            Logger.getLogger(SortEngine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void adcionatexto(String s) {
        try {
            this.view.getTxtSaida().getStyledDocument().insertString(this.view.getTxtSaida().getStyledDocument().getLength() + 1, s, null);
        } catch (BadLocationException ex) {
            Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void AtualizaBarra() {
        System.out.println("barra " + this.view.getPgbProgresso().getValue());
        this.view.getPgbProgresso().setValue(this.view.getPgbProgresso().getValue() + 1);
        if (this.view.getPgbProgresso().getValue() == this.view.getPgbProgresso().getMaximum()) {
            this.view.getPgbProgresso().setForeground(Color.green);
        }
        this.view.getPgbProgresso().repaint();
    }

    //private atualizaProgresso(){}
    private int[] geraVetor(int n) {
        int[] v = null;
        if (this.view.getChkAleatorios().isSelected()) {
            v = Vetor.geraVetorAletorio(n);
        }
        if (this.view.getChkCrescente().isSelected()) {
            v = Vetor.geraVetorOrdenadoCrescente(n);
        }
        if (this.view.getChkDecrescente().isSelected()) {
            v = Vetor.geraVetorOrdenadoDecrescente(n);
        }
        return v;
    }

    private int[] copiaVetor(int[] v) {
        int[] novo = new int[v.length];

        for (int i = 0; i < v.length; i++) {
            novo[i] = v[i];

        }

        return novo;
    }

    private void HeapSort(int[] vetor) {
        long tempoInicial = System.currentTimeMillis();
        HeapSort.sort(vetor);
        long tempoFinal = System.currentTimeMillis();
        adcionatexto("\n Heap:" + (tempoFinal - tempoInicial));
        AtualizaBarra();
    }

    private void MergeSort(int[] vetor) {
        long tempoInicial = System.currentTimeMillis();
        MergeSort.sort(vetor);
        long tempoFinal = System.currentTimeMillis();
        adcionatexto("\n Merge:" + (tempoFinal - tempoInicial));
        AtualizaBarra();
    }

    private void QuickSort(int[] vetor) {
        long tempoInicial = System.currentTimeMillis();
        QuickSort.sort(vetor);
        long tempoFinal = System.currentTimeMillis();
        adcionatexto("\n Quick:" + (tempoFinal - tempoInicial));
        AtualizaBarra();
    }

    private void BubbleSort(int[] vetor) {
        long tempoInicial = System.currentTimeMillis();
        SimpleSort.bubbleSort(vetor);
        long tempoFinal = System.currentTimeMillis();
        adcionatexto("\n Bubble:" + (tempoFinal - tempoInicial));
        AtualizaBarra();
    }

    private void InsertionSort(int[] vetor) {
        long tempoInicial = System.currentTimeMillis();
        SimpleSort.insertionSort(vetor);
        long tempoFinal = System.currentTimeMillis();
        adcionatexto("\n Insertion:" + (tempoFinal - tempoInicial));
        AtualizaBarra();
    }

    private void SelectionSort(int[] vetor) {
        long tempoInicial = System.currentTimeMillis();
        SimpleSort.selectionSort(vetor);
        long tempoFinal = System.currentTimeMillis();
        adcionatexto("\n Selection:" + (tempoFinal - tempoInicial));
        AtualizaBarra();
    }
}
